namespace Core.Features
{
    using System;
    using Base;
    using Contracts.Enums;
    using Resources;
    using System.Linq;

    
    public sealed class ArmorModuleCpuUsage<T> : FeatureBase<object>
    {
        public ArmorModuleCpuUsage(){}

        public ArmorModuleCpuUsage(T value) 
            : base(value){ }

        /// <summary>
        /// Obtains the feature name
        /// </summary>
        public override string Name
        {
            get { return "Cpu usage"; }
        }

        /// <summary>
        /// Obtains the feature group
        /// </summary>
        public override FeatureGroupsEnum FeatureGroup { get { return FeatureGroupsEnum.Engine; } }
        /// <summary>
        /// Obtains the feature type
        /// </summary>
        public override FeaturesEnum FeatureType { get { return FeaturesEnum.ArmorModuleCpuUsage; } }
        /// <summary>
        /// Obtains or defines the feature unit
        /// </summary>
        public override string Unit { get { return Traduction.label_unit_TF; } }

        public override void SetModifiedValue(Contracts.Interfaces.IAgent agent, Contracts.Interfaces.IRobot robot)
        {
            if (agent == null || robot == null) return;

            // Bonuses applied from extensions
            double modifValue = (from extension in agent.Extensions.Where(e => e.Modifiers != null && e.Modifiers.Any())
                                 let mods = extension.Modifiers.Where(m => m.ModifiedFeatureType == FeatureTypesEnum.Module && m.Feature == this.FeatureType)
                                 where mods.Any()
                                 let extLevel = extension.Level
                                 select mods.Sum(modifier => modifier.Value*extLevel)).Sum();

            

            // Bonuses applied from bot
            foreach (var bonusModifier in robot.Modifiers.Where(b => b.ModifiedFeatureType == FeatureTypesEnum.Module && b.Feature == this.FeatureType))
            {
                var bmod = bonusModifier;
                var bextension = agent.Extensions.Where(e => Equals(bmod.Extension, e)).FirstOrDefault();
                if (bextension == null) continue;

                modifValue += bmod.Value * bextension.Level;
            }

            ModifiedValue = IsRounded
                ? Math.Round((double)Value * (1 + modifValue), 0, MidpointRounding.AwayFromZero)
                : Math.Round((double)Value * (1 + modifValue), 2);
        }
    }
}